package com.ruoyi.web.controller.repair;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.HouseRepairCheckT;
import com.ruoyi.project.service.IHouseProjectTService;
import com.ruoyi.repairArea.domain.HouseRepairAreaT;
import com.ruoyi.repairArea.service.IHouseRepairAreaTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 户资金修改Controller
 *
 * @author tyq
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/repair/repairArea")
public class HouseRepairAreaTController extends BaseController {
    @Autowired
    private IHouseRepairAreaTService houseRepairAreaTService;

    private HouseDoorT doorVO = new HouseDoorT();
    private HouseProjectT projectVO = new HouseProjectT();

    private List<HouseRepairAreaT> houseRepairAreaList = new ArrayList<HouseRepairAreaT>();
    private List<HouseRepairAreaT> repairListTemp = new ArrayList<HouseRepairAreaT>();

    private List<HouseProjectT> houseProjectList = new ArrayList<HouseProjectT>();
    @Autowired
    private IHouseProjectTService houseProjectTService;
    @Autowired
    private IHouseDoorTService houseDoorTService;

    private String projectId;
    public HouseRepairAreaTController(IHouseProjectTService houseProjectTService) {
        this.houseProjectTService = houseProjectTService;
    }



    /**
     * 查询维修列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseRepairAreaT houseRepairAreaT) {
        HouseRepairAreaT houseRepairAreaVO=new HouseRepairAreaT();
        List<HouseRepairAreaT> list = new ArrayList<HouseRepairAreaT>();
        projectId= houseRepairAreaT.getProjectId();
        projectVO=houseProjectTService.selectHouseProjectTByProjectId(projectId);
        startPage();
        if(projectVO!=null && "1".equals(projectVO.getProjectSts())){
        }else{
            list = houseRepairAreaTService.selectHouseRepairAreaTList(houseRepairAreaT);
            if(getDataTableSumJudge(list)){
                houseRepairAreaVO = houseRepairAreaTService.selectHouseRepairAreaSum(houseRepairAreaT);
                houseRepairAreaVO.setAreaName("合计");
                list.add(houseRepairAreaVO);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出户资金修改列表
     */
    @Log(title = "小区维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseRepairAreaT houseRepairAreaT) {
        HouseRepairAreaT houseRepairAreaVO=new HouseRepairAreaT();
        List<HouseRepairAreaT> list = new ArrayList<HouseRepairAreaT>();
        projectId= houseRepairAreaT.getProjectId();
        projectVO=houseProjectTService.selectHouseProjectTByProjectId(projectId);
        if(projectVO!=null && "1".equals(projectVO.getProjectSts())){
        }else{
            list = houseRepairAreaTService.selectHouseRepairAreaTList(houseRepairAreaT);
            if(getDataTableSumJudge(list)){
                houseRepairAreaVO = houseRepairAreaTService.selectHouseRepairAreaSum(houseRepairAreaT);
                houseRepairAreaVO.setAreaName("合计");
                list.add(houseRepairAreaVO);
            }
        }
        ExcelUtil<HouseRepairAreaT> util = new ExcelUtil<HouseRepairAreaT>(HouseRepairAreaT.class);
        util.exportExcel(response, list, "户资金修改数据");
    }

    /**
     * 获取户资金修改详细信息
     */

    @GetMapping(value = "/{houseId}/{projectId}")
    public AjaxResult getInfo(@PathVariable("houseId") String houseId,@PathVariable("projectId") String projectId) {
        HouseRepairAreaT houseRepairAreaVO = new HouseRepairAreaT();
        houseRepairAreaVO.setHouseId(houseId);
        houseRepairAreaVO.setProjectId(projectId);
        return AjaxResult.success(houseRepairAreaTService.getRepairDetail(houseRepairAreaVO));
    }


    /**
     * 新增户资金修改
     */
    @Log(title = "户资金修改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseRepairAreaT houseRepairAreaT) {
        return toAjax(houseRepairAreaTService.insertHouseRepairAreaT(houseRepairAreaT));
    }

    /**
     * 修改户资金修改
     */
    @Log(title = "户资金修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseRepairAreaT houseRepairAreaT) {
        HouseRepairAreaT houseRepairAreaVO = new HouseRepairAreaT();
        HouseRepairCheckT houseRepairCheckO = new HouseRepairCheckT();
        HouseProjectT houseProjectT = new HouseProjectT();
        houseRepairAreaVO = houseRepairAreaTService.getRepairDetail(houseRepairAreaT);
        houseProjectT.setProjectId(houseRepairAreaT.getProjectId());
        houseProjectT.setRepairMoney(houseRepairAreaVO.getRepairMoney());
        houseProjectT.setCheckMoney(houseRepairAreaT.getRepairMoney());
        houseRepairAreaVO.setRepairMoney(houseRepairAreaT.getRepairMoney());
        if(houseRepairAreaT.getRepairMoney() == 0){
            houseRepairAreaVO.setNoMoney(0d);
        }else if(houseRepairAreaVO.getBalance() <= houseRepairAreaVO.getBudgetMoney()*0.3){
            houseRepairAreaVO.setNoMoney(houseRepairAreaT.getRepairMoney());
        }else if(houseRepairAreaVO.getBalance() - houseRepairAreaT.getRepairMoney() >= houseRepairAreaVO.getBudgetMoney()*0.3){
            houseRepairAreaVO.setNoMoney(0d);
        }else{
            houseRepairAreaVO.setNoMoney(DataUtils.doubleScale2(houseRepairAreaT.getRepairMoney() - (houseRepairAreaVO.getBalance() - houseRepairAreaVO.getBudgetMoney()*0.3)) );
        }
        houseProjectT.setNoMoney(houseRepairAreaVO.getNoMoney());
        houseRepairCheckO.setHouseId(houseRepairAreaVO.getHouseId());
        houseRepairCheckO.setProjectId(houseRepairAreaVO.getProjectId());
        houseRepairCheckO.setRepairMoney(houseRepairAreaVO.getRepairMoney());
        houseRepairCheckO.setUpdateMoney(houseRepairAreaT.getRepairMoney());
        houseRepairCheckO.setUpdater(getUsername());

        return toAjax(houseRepairAreaTService.updateHouseRepairAreaT(houseRepairAreaVO,houseRepairCheckO,houseProjectT));
    }

    /**
     * 删除户资金修改
     */
    @Log(title = "户资金修改", businessType = BusinessType.DELETE)
    @DeleteMapping("/{doorIds}")
    public AjaxResult remove(@PathVariable Long[] doorIds) {
        return toAjax(houseRepairAreaTService.deleteHouseRepairAreaTByDoorIds(doorIds));
    }

    /**
     *试算
     */
    @GetMapping (value = "/getCalculate")
    public AjaxResult getCalculateByProjectId(HouseProjectT houseProjectT) {
        HouseProjectT houseProjectVO = new HouseProjectT();
        HouseRepairAreaT houseRepairAreaVO = new HouseRepairAreaT();
        Double trueMoney = 0d;
        String projectId = houseProjectT.getProjectId();
        Double price = houseProjectT.getPrice();
        houseProjectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
        houseRepairAreaVO.setProjectId(projectId);
        List<HouseRepairAreaT> houseRepairAreaList = houseRepairAreaTService.selectHouseRepairAreaTList(houseRepairAreaVO);
        doorVO.setAreaName(houseProjectVO.getAreaName());
        doorVO = houseDoorTService.getBalanceMoneyAll(doorVO);
        if(price==null){
            price= DataUtils.divideMoney(houseProjectVO.getProjectMoney(),doorVO.getSpace());
        }
//        BigDecimal price =(BigDecimal.valueOf(houseProjectVO.getProjectMoney())).divide(BigDecimal.valueOf(doorVO.getSpace()),6,BigDecimal.ROUND_HALF_UP);
        for (HouseRepairAreaT vo : houseRepairAreaList){
            if(vo.getSpace()==0){
                String houseid=vo.getHouseId();
                return AjaxResult.error(houseid+"该房屋编号面积为0,不能产生分摊");
            }
            Double repairMoney= DataUtils.spaceMoney(vo.getSpace(),price);
            trueMoney = DataUtils.sumMoney(trueMoney,  repairMoney);
        }
        houseProjectVO.setTrueMoney(trueMoney);
        houseProjectVO.setPrice(price);
        houseProjectVO.setSpace(doorVO.getSpace());
        return AjaxResult.success(houseProjectVO);
    }

    /**
     * 核算
     */
    @Log(title = "核算", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCalculate")
    public AjaxResult updateCalculate(@RequestBody HouseProjectT houseProjectT) {
        HouseRepairAreaT houseRepairAreaVO = new HouseRepairAreaT();
        houseRepairAreaVO.setProjectId(houseProjectT.getProjectId());
        Double price = Double.valueOf(houseProjectT.getPrice());
        Double trueMoney = 0d;
        List<HouseRepairAreaT> houseRepairAreaList = houseRepairAreaTService.selectHouseRepairAreaTList(houseRepairAreaVO);
        for(HouseRepairAreaT vo:houseRepairAreaList){
            HouseRepairAreaT temp =new HouseRepairAreaT();
            Double repaireMoney = 0d;
            repaireMoney = DataUtils.doubleScale2(vo.getSpace()*price);
            if(vo.getBalance()- vo.getBudgetMoney()*0.3<=0){
                temp.setNoMoney(DataUtils.doubleScale2(repaireMoney));
            }else if(vo.getBalance()- vo.getBudgetMoney()*0.3>=repaireMoney){
                temp.setNoMoney(0d);
            }else{
                temp.setNoMoney(DataUtils.doubleScale2(repaireMoney-vo.getBalance()+vo.getBudgetMoney()*0.3));
            }
            temp.setProjectId(houseRepairAreaVO.getProjectId());
            temp.setHouseId(vo.getHouseId());
            temp.setRepairMoney(repaireMoney);
            temp.setPayMoney(repaireMoney-temp.getNoMoney());
            trueMoney = DataUtils.sumMoney(trueMoney,  repaireMoney);
            repairListTemp.add(temp);
        }
        projectVO.setTrueMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setPayMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setProjectId(houseRepairAreaVO.getProjectId());
        houseRepairAreaTService.updateProjectAndRepair(projectVO,repairListTemp);

        return toAjax(1);
    }


}
